using System.Collections.Generic;
using Game.Networking.Client;
using Utils;

namespace Game.UI {
	public class ClientComputingUnitUserDataProvider : IComputingUnitUserDataProvider {
		public ClientScriptingController CS => ClientScriptingController.Instance;

		public EventBus OnCodeFileListUpdated => CS.OnCodeFileListUpdated;

		public EventBus<string> OnCodeFileUpdated => CS.OnFileUpdateReceived;

		public EventBus OnUnitStateChanged => CS.OnUnitStateChanged;

		public EventBus OnTerminalCommandReceived => CS.OnTerminalCommandReceived;

		public EventBus<uint> OnAttachedEntityIdChanged => CS.OnAttachedEntityIdChanged;

		public void CreateNewCodeFile(string fileName, string contents) {
			CS.CreateNewCodeFile(fileName, contents);
		}

		public List<string> GetCodeFiles() {
			return CS?.GetCodeFileList();
		}

		public ComputingUnitState GetCurrentUnitState() {
			return CS?.GetUnitState() ?? ComputingUnitState.Uninitialized;
		}

		public string GetFileContents(string fileName) {
			return CS?.GetCodeFileContents(fileName);
		}

		public List<string> GetTerminalMessages() {
			return CS?.TerminalLines;
		}

		public bool IsCodeFileDownloaded(string fileName) {
			return CS?.HasDownloadedCodeFile(fileName) ?? false;
		}

		public void SaveCodeFile(string fileName, string contents) {
			CS?.SaveAndUploadCodeFile(fileName, contents);
		}

		public void SendRecompileRequest() {
			CS?.SendRecompileRequest();
		}

		public void SendTerminalMessage(string message) {
			CS?.SendTerminalMessage(message);
		}

		public void TryDownloadCodeFile(string fileName) {
			CS?.TryDownloadFile(fileName);
		}

		public uint GetAttachedEntityId() {
			return CS?.GetAttachedEntityId() ?? 0;
		}

		public void TryRemoveFile(string fileName) {
			CS?.TryRemoveFile(fileName);
		}

		public ShipCore GetAttachedEntity() {
			var id = GetAttachedEntityId();
			if ( id == 0 ) {
				return null;
			}

			var e = ClientEntityController.Instance.GetEntityById(id);
			if ( e == null ) {
				return null;
			}

			var ship = (ShipCore)e;
			if ( ship == null ) {
				return null;
			}

			return ship;
		}

		public void SaveCodeFileLocally(string fileName, string contents) {
			CS?.SetCodeFile(fileName, contents);
		}

		public void UploadFiles() {
			CS?.UploadAllCodeFiles();
		}

		public void DownloadFiles() {
			CS?.TryDownloadAllFiles();
		}
	}
}